/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import com.google.common.collect.ImmutableList;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.panteleyev.blogclient.BloggerEditorFrame;
import org.panteleyev.blogclient.IPicturePublishDialog;
import org.panteleyev.blogclient.PublishTargetPanel;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SwingFactory;

/**
 *
 * @author Petr Panteleyev
 */
final class GPicturePublishDialog extends BasicDialog<JFrame> implements IPicturePublishDialog {
    private ImmutableList<GPhoto> pictures;

    private JComboBox<String>   sizeComboBox;
    private JComboBox<String>   columnsComboBox;
    private JRadioButton        fullSizeRadioButton;
    private JRadioButton        reducedSizeRadioButton;
    private JCheckBox           borderCheckBox;
    private JCheckBox           linkToOriginalCheckBox;
    private JCheckBox           _imgBorderCheckBox;

    private PublishTargetPanel targetPanel;

    public GPicturePublishDialog(JFrame parent, ImmutableList<GPhoto> pictures) {
        super(parent);
        this.pictures = pictures;

        initComponents();

        fullSizeRadioButton.setSelected(true);
        enableSizeControls(false);

        pack();
    }

    private void initComponents() {
        setTitle(L10N.PICTURE_PUBLISH_DIALOG_TITLE.s());
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));

        getContentPane().add(SwingFactory.createPanel(
            BorderFactory.createTitledBorder(L10N.PPD_TABLE_BORDER_LABEL.s()), SwingFactory.LAYOUT.BOX_X,
            new JLabel(COLUMNS),
            Box.createHorizontalStrut(3),
            columnsComboBox = new JComboBox<>(new String[] { "1", "2", "3", "4" }),
            Box.createHorizontalStrut(5),
            borderCheckBox = new JCheckBox(L10N.PPD_BORDER_LABEL.s()),
            Box.createHorizontalGlue()
        ));

        JPanel panel;
        getContentPane().add(panel = SwingFactory.createPanel(
            BorderFactory.createTitledBorder(LINK), SwingFactory.LAYOUT.BOX_Y,
            fullSizeRadioButton = new JRadioButton(FULL_SIZE),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                reducedSizeRadioButton = new JRadioButton(REDUCED_SIZE),
                Box.createHorizontalStrut(3),
                sizeComboBox = new JComboBox<>(),
                Box.createHorizontalGlue()
            ),
            linkToOriginalCheckBox = new JCheckBox(LINK_TO_ORIGINAL),
            _imgBorderCheckBox = new JCheckBox(L10N.PPD_BORDER_LABEL.s())
        ));
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);

        List<MediaThumbnail> thumbs = pictures.get(0).getThumbnails();
        for (MediaThumbnail t : thumbs) {
            sizeComboBox.addItem(new StringBuilder().append(t.getWidth()).append("x").append(t.getHeight()).toString());
        }

        ButtonGroup grp = new ButtonGroup();
        grp.add(fullSizeRadioButton);
        grp.add(reducedSizeRadioButton);

        getContentPane().add(targetPanel = new PublishTargetPanel());

        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);
        getContentPane().add(new OkCancelButtonPanel(this));
        SwingFactory.setAlignmentX(getContentPane(), LEFT_ALIGNMENT);
        SwingFactory.setWidthMaximumToPreferred(sizeComboBox);

        fullSizeRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                enableSizeControls(false);
            }
        });

        reducedSizeRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                enableSizeControls(true);
            }
        });
    }

    int getColumnCount() {
        return columnsComboBox.getSelectedIndex() + 1;
    }

    boolean getFullSize() {
        return fullSizeRadioButton.isSelected();
    }

    boolean getLinkToSource() {
        return linkToOriginalCheckBox.isSelected();
    }

    String getSmallSize() {
        return (String)sizeComboBox.getSelectedItem();
    }

    int getSmallSizeIndex() {
        return sizeComboBox.getSelectedIndex();
    }

    boolean getBorder() {
        return borderCheckBox.isSelected();
    }

    private void enableSizeControls(boolean enable) {
        sizeComboBox.setEnabled(enable);
        linkToOriginalCheckBox.setEnabled(enable);
    }

    BloggerEditorFrame<?> getDestinationWindow() {
        return targetPanel.getDestinationWindow();
    }

    boolean getImageBorder() { return _imgBorderCheckBox.isSelected(); }
}
